// Seir programming Editor
// Copyright (C) 2007 David Torres
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

/*
 * Created by SharpDevelop.
 * User: David William Torres
 * Date: 5/11/2007
 * Time: 11:00 PM
 * 
 */

using System;
using System.IO;
using Gtk;
using GtkSourceView;

namespace Seir
{
	/// <summary>
	/// Interface for main window.  All initialization of widgets are handled
	/// internally.  
	/// </summary>
	public class sInterface
	{
		// 
		Window win;
		VBox box;
		MenuBar mb;
		ScrolledWindow sw;
		SourceView src;
		SourceBuffer buffer;
		Statusbar sb;
		//StreamReader sr;
		
		//static string filename;
		
		public sInterface()
		{
			// Main window init
			win = new Window("Seir Code Editor");
            win.DefaultWidth = 400;
            win.DefaultHeight = 300;
            win.DeleteEvent += delete_event;  //Handler for closing the App, method defined below
            
            win.Add(CreateView());
            win.ShowAll();
		}
		
		// Creates a framework for the window layout.  
		VBox CreateView ()
		{
			// Vertical frames for widgets
            box = new VBox (false, 3);
            
            // Menu init
            mb = new MenuBar();
			Menu file_menu = new Menu();
			MenuItem new_item = new MenuItem("New");
			MenuItem open_item = new MenuItem("Open");
			MenuItem save_item = new MenuItem("Save");
			MenuItem save_as_item = new MenuItem("Save As");
			MenuItem exit_item = new MenuItem("Exit");
			
			exit_item.Activated += new EventHandler(exit_app);
			open_item.Activated += new EventHandler(open_file);
			
			// Attach menu items to File menu
			file_menu.Append (new_item);
			file_menu.Append (open_item);
			file_menu.Append (save_item);
			file_menu.Append (save_as_item);
			file_menu.Append (exit_item);
			
			// create submenu and place menu in editor
			MenuItem file_item = new MenuItem("File");
			file_item.Submenu = file_menu;
			mb.Append (file_item);
			box.PackStart(mb, false, false, 0);
			
			//Menu edit_menu = new Menu();  // TODO add edit menu
            
			// Create a window with automatic scrolling to hold text/source view
            sw = new ScrolledWindow();
            src = new SourceView(CreateBuffer()); // Text Editor component
            src.ShowLineNumbers = true;
            src.AutoIndent = true;
            
            sw.Add(src);
            box.PackStart(sw, true, true, 0);
            
            sb = new Statusbar();
            box.PackStart(sb, false, false, 0);
            
			return box;
		}
		
		//  Not necessary initially since a default buffer is created. 
		SourceBuffer CreateBuffer ()
		{
			SourceLanguagesManager manager = new SourceLanguagesManager ();
			SourceLanguage lang = manager.GetLanguageFromMimeType ("text/x-csharp");

			buffer = new SourceBuffer (lang);
			buffer.Highlight = true;
			//sr = File.OpenText (filename);
			//buffer.Text = sr.ReadToEnd ();
			//sr.Close ();
			return buffer;
		}
		
		static void delete_event(object obj, DeleteEventArgs args)
        {
            Application.Quit();
        }
		static void exit_app(object obj, EventArgs args)
		{
			Application.Quit ();
		}
		
		protected virtual void open_file(object obj, EventArgs args)
     	{
         	Gtk.FileChooserDialog fc = 
          	new FileChooserDialog("Choose the file to open",
                                      win,
                                      FileChooserAction.Open,
                                      "Cancel",ResponseType.Cancel,
                                      "Open",ResponseType.Accept);

          	if (fc.Run() == (int)ResponseType.Accept) 
          	{
               	TextReader file = new StreamReader (fc.Filename);
               	src.Buffer.Text = file.ReadToEnd();
          	}
          	fc.Destroy();
     	}
	}
}
